home *** CD-ROM | disk | FTP | other *** search
/ Pascal Super Library / Pascal Super Library (CW International)(1997).bin / TURB_VIS / MACROS / MACDEMO.PAS < prev    next >
Pascal/Delphi Source File  |  1992-01-31  |  8KB  |  289 lines

  1. {
  2.   Very simple application to demonstrate Macros. Documented to show the
  3.   modifications you will need to make to your application to implement
  4.   macros.
  5. }
  6.  
  7. Program MacDemo;
  8.  
  9. {$DEFINE MACROS}         {implement macros}
  10.  
  11. {$X+}
  12.  
  13.   {------------------------ Macros Modification ----------------------------}
  14.   { Include Macros on your Uses line. (MacHelp is added for this demo).     }
  15.   {-------------------------------------------------------------------------}
  16. Uses Objects, Drivers, Views, Menus, App, HelpFile, MsgBox
  17. {$IFDEF MACROS}
  18. ,Macros,
  19. MacHelp
  20. {$ENDIF}
  21. ;
  22.  
  23. const
  24.    cmRecordMacro     = 1500;
  25.    cmStopRecording   = 1501;
  26.    cmPlaybackMacro   = 1502;
  27.    cmDeleteMacro     = 1503;
  28.  
  29.  
  30. Type
  31.  
  32.   {------------------------ Macros Modification ----------------------------}
  33.   { MyApp is an object of type TMacApp instead of TApplication.             }
  34.   {-------------------------------------------------------------------------}
  35.   PMyApp = ^MyApp;
  36. {$IFDEF MACROS}
  37.   MyApp = Object(TMacApp)
  38. {$ELSE}
  39.   MyApp = Object(TApplication)
  40. {$ENDIF}
  41.     Constructor Init;
  42.     Procedure InitStatusLine; Virtual;
  43.     Procedure InitMenuBar; Virtual;
  44.     Procedure HandleEvent(var Event: TEvent); Virtual;
  45. {$IFDEF MACROS}
  46.     Procedure GetEvent(var Event: TEvent); virtual;
  47. {$ENDIF}
  48.     function GetPalette: PPalette; virtual;
  49.     Procedure NewWindow;
  50.     Destructor Done; Virtual;
  51.   End;
  52.  
  53.   PMyStatusLine = ^TMyStatusLine;
  54.   TMyStatusLine = object(TStatusLine)
  55.     function Hint(AHelpCtx: Word): String; virtual;
  56.   end;
  57.  
  58.   PMyWindow = ^TMyWindow;
  59.   TMyWindow = Object(TWindow)
  60.      Constructor Init(Bounds: TRect; WinTitle: String; WindowNo: Word);
  61.   End;
  62.  
  63.   PMyDeskTop = ^TMyDeskTop;
  64.   TMyDeskTop = object(TDeskTop)
  65.      Windw: PMyWindow;
  66.   End;
  67.  
  68. Const
  69.   cmNewWin   = 101;
  70.  
  71.  
  72. function TMyStatusLine.Hint(AHelpCtx: Word): String;
  73. var Mem : string;
  74. begin
  75.   str(MemAvail, Mem);
  76.   Hint :=  Mem + ' free';
  77. end;
  78.  
  79.  
  80. { ** MyApp **}
  81. Constructor MyApp.Init;
  82. Begin
  83.  
  84.   {------------------------ Macros Modification ----------------------------}
  85.   { Call TMacApp.Init instead of TApplication.Init                          }
  86.   {-------------------------------------------------------------------------}
  87. {$IFDEF MACROS}
  88.   TMacApp.Init;
  89. {$ELSE}
  90.   TApplication.Init;
  91. {$ENDIF}
  92.   RegisterHelpFile;
  93. End;
  94.  
  95. Destructor MyApp.Done;
  96. Begin
  97.  
  98.   {------------------------ Macros Modification ----------------------------}
  99.   { Call TMacApp.Done instead of TApplication.Done                          }
  100.   {-------------------------------------------------------------------------}
  101. {$IFDEF MACROS}
  102.   TMacApp.Done;
  103. {$ELSE}
  104.   TApplication.Done;
  105. {$ENDIF}
  106. End;
  107.  
  108. function MyApp.GetPalette: PPalette;
  109. const
  110.   CNewColor = CColor + CHelpColor;
  111.   CNewBlackWhite = CBlackWhite + CHelpBlackWhite;
  112.   CNewMonochrome = CMonochrome + CHelpMonochrome;
  113.   P: array[apColor..apMonochrome] of string[Length(CNewColor)] =
  114.     (CNewColor, CNewBlackWhite, CNewMonochrome);
  115. begin
  116.   GetPalette := @P[AppPalette];
  117. end;
  118.  
  119. Procedure MyApp.InitMenuBar;
  120. Var
  121.   R: TRect;
  122. Begin
  123.   GetExtent(R);
  124.   R.B.Y := R.A.Y + 1;
  125.   MenuBar := New(PMenuBar, Init(R, NewMenu(
  126.     NewSubMenu('~F~ile', hcNoContext, NewMenu(
  127.       NewItem('~N~ew', 'F4', kbF4, cmNewWin, hcNoContext,
  128.       NewItem('~S~ize/Move', 'Ctrl-F5', kbCtrlF5, cmResize, 1051,
  129.     nil))),
  130.  
  131.  
  132.   {------------------------ Macros Modification ----------------------------}
  133.   { Add code below to your application's menu.                              }
  134.   {-------------------------------------------------------------------------}
  135. {$IFDEF MACROS}
  136.     NewSubMenu('~M~acros', {0}hcMacros, NewMenu(
  137.       NewItem('~R~ecord', '', kbNoKey, cmRecordMacro, hcRecording,
  138.       NewItem('~S~top Recording', '', kbNoKey, cmStopRecording, hcRecording,
  139.       NewItem('~D~elete', '', kbNoKey, cmDeleteMacro, hcDeleting,
  140.       NewItem('~P~layback', '', kbNoKey, cmPlaybackMacro, hcPlayback,
  141.     nil))))),
  142. {$ENDIF}
  143.  
  144.  
  145.       NewLine(
  146.         NewItem('E~x~it', 'Alt-X', kbAltX, cmQuit, hcNoContext,
  147.       nil))))))
  148. {$IFDEF MACROS}
  149.       )
  150. {$ENDIF}
  151.       ;
  152. End;
  153.  
  154. {$IFDEF MACROS}
  155. procedure MyApp.GetEvent(var Event: TEvent);
  156. var
  157.   WH: PWindow;
  158.   HFile: PHelpFile;
  159.   HelpStrm: PDosStream;
  160. const
  161.   HelpInUse: Boolean = False;
  162. begin
  163.  
  164.   {------------------------ Macros Modification ----------------------------}
  165.   { Calls TMacApp.GetEvent instead of TApplication.GetEvent.                }
  166.   { This change is required only if your application overrides GetEvent to  }
  167.   { implement context-sensitive help (as done in TVDEMO.PAS).               }
  168.   {-------------------------------------------------------------------------}
  169.   TMacApp.GetEvent(Event);
  170.   {TApplication.GetEvent(Event);}
  171.  
  172.   case Event.What of
  173.     evCommand:
  174.       if (Event.Command = cmHelp) and not HelpInUse then
  175.       begin
  176.         HelpInUse := True;
  177.  
  178.   {------------------------ Macros Modification ----------------------------}
  179.   { The name of the Macros Help file is here, instead of your help file, in }
  180.   { order to supply help for this demo.                                     }
  181.   {-------------------------------------------------------------------------}
  182.         HelpStrm := New(PDosStream, Init('MACHELP.HLP', stOpenRead));
  183.  
  184.         HFile := New(PHelpFile, Init(HelpStrm));
  185.         if HelpStrm^.Status <> stOk then
  186.         begin
  187.           MessageBox('Could not open help file.', nil, mfError + mfOkButton);
  188.           Dispose(HFile, Done);
  189.         end
  190.         else
  191.         begin
  192.           WH := New(PHelpWindow,Init(HFile, GetHelpCtx));
  193.           if ValidView(WH) <> nil then
  194.           begin
  195.             ExecView(WH);
  196.             Dispose(WH, Done);
  197.           end;
  198.           ClearEvent(Event);
  199.         end;
  200.         HelpInUse := False;
  201.       end;
  202.     evMouseDown:
  203.       if Event.Buttons <> 1 then Event.What := evNothing;
  204.   end;
  205. end;
  206. {$ENDIF}
  207.  
  208.  
  209. Procedure MyApp.InitStatusLine;
  210. Var
  211.   R: TRect;
  212. Begin
  213.   GetExtent(R);
  214.   R.A.Y := R.B.Y - 1;
  215.   StatusLine := New(PMyStatusLine, Init(R,
  216.   NewStatusDef(0, $FFFF,
  217.     NewStatusKey('~F1~ Help', kbF1, cmHelp,
  218.     NewStatusKey('', kbF10, cmMenu,
  219.     NewStatusKey('~Alt-X~ Exit', kbAltX, cmQuit,
  220.     NewStatusKey('~F4~ New', kbF4, cmNewWin,
  221.     NewStatusKey('~Alt-F3~ Close', kbAltF3, cmClose,
  222.     nil))))),
  223.   nil)));
  224. End;
  225.  
  226. Procedure MyApp.HandleEvent(var Event: TEvent);
  227. Begin
  228.   TApplication.HandleEvent(Event);
  229.   if Event.What = evCommand then
  230.     Begin
  231.       Case Event.Command of
  232.         cmNewWin: NewWindow;
  233.  
  234.  
  235.   {------------------------ Macros Modification ----------------------------}
  236.   { Add code block below to your application's event handler.               }
  237.   {-------------------------------------------------------------------------}
  238. {$IFDEF MACROS}
  239.         cmRecordMacro   : StartRecording;
  240.         cmStopRecording : begin
  241.                             StopRecording;
  242.  
  243.                             ClearEvent (Event);     { ********************* }
  244.                             PutEvent (Event);       { Clears event queue.   }
  245.                                                     { Required for macros   }
  246.                                                     { that playback other   }
  247.                                                     { macros.               }
  248.                           end;
  249.         cmPlaybackMacro : StartPlayback;
  250.         cmDeleteMacro   : DeleteMacro;
  251. {$ENDIF}
  252.  
  253.  
  254.       else
  255.         Exit;
  256.       End;
  257.       ClearEvent(Event);
  258.     End;
  259. End;
  260.  
  261. Procedure MyApp.NewWindow;
  262. Var
  263.   Window:PMyWindow;
  264.   R: TRect;
  265. Begin
  266.   R.Assign(0, 0, 24, 7);
  267.   R.Move(Random(55), Random(16));
  268.   Window := New(PMyWindow, Init(R, 'Demo Window', 1));
  269.   DeskTop^.Insert(Window);
  270. End;
  271.  
  272. { ** MyDeskTop **}
  273.  
  274. { ** MyWindow **}
  275. Constructor TMyWindow.Init(Bounds: TRect; WinTitle: String; WindowNo: Word);
  276. Begin
  277.   TWindow.init(Bounds,WinTitle,WindowNo);
  278. End;
  279.  
  280. { Main Program }
  281. Var
  282.   MyTApp:MyApp;
  283.  
  284. Begin
  285.   MyTApp.Init;
  286.   MyTApp.Run;
  287.   MyTApp.Done;
  288. End.
  289.